﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAssignIndexedCMDenseMatrix.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.CMDenseMatrixTest
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;

    using NUnit.Framework;

    #endregion

    /// <summary>
    ///     Test suite for
    ///     <see cref="CMDenseMatrixExtensions.Assign{T}(CMDenseMatrix{T},int[],int[],CMDenseMatrix{T})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.CMDenseMatrixExtensions")]
    public sealed class TestAssignIndexedCMDenseMatrix
    {
        /// <summary>
        ///     Tests the method when indexes of target elements are non-negative and less than size of matrix dimension.
        /// </summary>
        [Test]
        public void WhenTargetIndexesAreValid()
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 1, 10, 20 },
                                     new double[] { 4, 5, 6 },
                                     new double[] { 7, 30, 40 }
                                 };

            // Act
            target.Assign(
                targetRowsIndexes: new[] { 1, 2 },
                targetColumnsIndexes: new[] { 0, 2 },
                source: source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when targetRowsIndexes parameter is empty collection.
        /// </summary>
        [Test]
        public void WhenRowsIndexesCollectionIsEmpty()
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 1, 2, 3 },
                                     new double[] { 4, 5, 6 },
                                     new double[] { 7, 8, 9 }
                                 };

            // Act
            target.Assign(
                targetRowsIndexes: new int[0],
                targetColumnsIndexes: new[] { 0, 2 },
                source: source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when targetColumnsIndexes parameter is empty collection.
        /// </summary>
        [Test]
        public void WhenColumnsIndexesCollectionIsEmpty()
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });
            var expectedResult = new[]
                                 {
                                     new double[] { 1, 2, 3 },
                                     new double[] { 4, 5, 6 },
                                     new double[] { 7, 8, 9 }
                                 };

            // Act
            target.Assign(
                targetRowsIndexes: new[] { 1, 2 },
                targetColumnsIndexes: new int[0],
                source: source);

            // Assert
            Assert.That(target.Values, Is.EquivalentTo(expectedResult));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when target parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendIsNull()
        {
            // Arrange
            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new[] { 0, 2 };
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });

            // Act and Assert
            Assert.That(
                () => CMDenseMatrixExtensions.Assign(null, targetRowsIndexes, targetColumnsIndexes, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("target"));
        }

        /// <summary>
        ///     Tests the method when source parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenSourceIsNull()
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });
            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => target.Assign(targetRowsIndexes, targetColumnsIndexes, null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("source"));
        }

        /// <summary>
        ///     Tests the method when targetRowsIndexes parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetRowsIndexesCollectionIsNull()
        {
            // Arrange
            var targetColumnsIndexes = new[] { 0, 2 };
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });

            // Act and Assert
            Assert.That(
                () => target.Assign(null, targetColumnsIndexes, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("targetRowsIndexes"));
        }

        /// <summary>
        ///     Tests the method when targetColumnsIndexes parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenTargetColumnsIndexesCollectionIsNull()
        {
            // Arrange
            var targetRowsIndexes = new[] { 1, 2 };
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });

            // Act and Assert
            Assert.That(
                () => target.Assign(targetRowsIndexes, null, source),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("targetColumnsIndexes"));
        }

        /// <summary>
        ///     Tests the method when a target row index is negative or greater than or equal to the number of rows
        ///     of target matrix.
        /// </summary>
        /// <param name="rowIndex">
        ///     The row index.
        /// </param>
        /// <param name="location">
        ///     The location of row index in the storage.
        /// </param>
        [Test]
        public void WhenATargetRowIndexIsOutOfRange([Values(-1, 3)] int rowIndex, [Values(0, 1)] int location)
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });

            var targetRowsIndexes = new int[2];
            targetRowsIndexes[location] = rowIndex;

            var targetColumnsIndexes = new[] { 0, 2 };
            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "targetRowsIndexes[{0}]", location);
            Assert.That(
                () => target.Assign(targetRowsIndexes, targetColumnsIndexes, source),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

        /// <summary>
        ///     Tests the method when a target column index is negative or greater than or equal to
        ///     the number of columns of target matrix.
        /// </summary>
        /// <param name="columnIndex">
        ///     The column index.
        /// </param>
        /// <param name="location">
        ///     The location of column index.
        /// </param>
        [Test]
        public void WhenColumnIndexIsOutOfRange([Values(-1, 3)] int columnIndex, [Values(0, 1)] int location)
        {
            // Arrange
            var target = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 1, 2, 3 },
                    new double[] { 4, 5, 6 },
                    new double[] { 7, 8, 9 }
                });

            var targetRowsIndexes = new[] { 1, 2 };
            var targetColumnsIndexes = new int[2];
            targetColumnsIndexes[location] = columnIndex;

            var source = new CMDenseMatrix<double>(
                new[]
                {
                    new double[] { 10, 20 },
                    new double[] { 30, 40 }
                });

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "targetColumnsIndexes[{0}]", location);
            Assert.That(
                () => target.Assign(targetRowsIndexes, targetColumnsIndexes, source),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

#endif
    }
}